home *** CD-ROM | disk | FTP | other *** search
- PLOTTING GRAPHS WITH LaTeX
- --------------------------
- sunil podar (podar@sbcs.csnet)
- ...!{allegra, philabs}!podar!sbcs
- last update: Feb 2, 1987 (fixed bugs: wasn't doings things right when (0,0)
- was not on the graph. Also removed the printing of a few of the
- margin commands from preamble. Added some more defaults.
- last update: Oct 5, 1986
- Added the ability to plot any quadrant.
- Changed the characters numbers from digits to Capital Letters.
- removed "X --->". Now prints X and Y near the axes vector-heads.
- Some parameters changed: NEW: %picdimens,%[XY]deltab:n,%[XY]orig-num
- DELETED: %picheight,%piclength,%want-X^Y^,%[XY]deltabar,%[XY]deltanum
- last update: Apr 14 1985 original version.
-
-
- This program provides an integrated (somewhat) system to plot graphs. Latex
- has a fairly powerful pic environment but it lacks any direct graph
- environment, and this program attempts to simulate one.
-
- You must have some knowledge of LaTeX, and in particular the pic environment
- if there is any need to change the output of this program. Actually if you
- know only the pic environment, you should be able to manage.
-
- The program can produce the graph as a complete document if you desire. The
- idea is that even non-LaTeX users can plot graphs and only graphs and it
- doesn't have to be part of regular text.
-
- The program, using the options given in the parameter list, produces a LaTeX
- file using all known-to-work and regular LaTeX commands and the file can
- easily be edited if needed. So, the moral is that one can plot graphs with
- being quite oblivious to the fact that LaTeX is being used to plot it; of
- course, you have to run LaTeX on the graph to print it.
-
- Please feel free to contact me (podar@sbcs) if there are any bugs, if it
- doesn't do what you want and can't figure out how to do it, or would like
- some other features in the program. I have not tested the program extensively
- so I can't promise it will work if you try to stretch it to its limits. I do
- not have complicated error-checking or error-recovery. If you make any
- modifications to this program, please let me know.
-
- The program generates LaTeX commands to do the following:
-
- 1. print the X-axis vector.
- 2. print the Y-axis vector.
- 3. put little vertical bars on the X-axis at regular intervals.
- 4. put little horizontal bars on the Y-axis at regular intervals.
- 5. put numbers along those bars on both axes.
- 6. prints "X" and "Y" along the axes.
- 7. sets up a legend box with info about X-axis, Y-axis and the various
- characters plotted on the graph; controlled by a variety of options.
- 8. reads input data in 3-tuples (character# X-coordinate Y-coordinate),
- scales the data according to given scale and generates commands to plot
- it. If there is no data, then it simply produces the skeleton graph.
-
-
- some quirks, limitations, suggestions, ideas regarding the program
- ------------------------------------------------------------------
- The program is not a very powerful program and the code is rather sloppy,
- but I attempt to do things right as far as possible
- and if not then I give pointers to how one might have to edit the output
- file. Some of the commands that I generate are highly dependent on the font
- being used, the unitlength etc., so I can only make a reasonable attempt to
- get things right. In order to use this program, I suggest that
- you print out the file produced by the program and then see what needs
- fixing; in most cases the fixing will be limited to changing a few
- coordinates of some commands.
-
- You can of course use the program to generate a dummy skeleton graph even if
- all you need is a few things out of the 8 steps listed above and extract
- out of the file the desired portion. The format of the graph is my personal
- preference and differs from other graph plotting packages that plot the
- graph within a bounded rectangle (such as graph(1) of unix.)
-
- The program may not give you what you desire but there are ways of
- still exploiting it if what you need is not what it can handle. For
- instance, you can use it to translate input data (the coordinate pairs)
- to the "\put" statements with desired scaling factors.
-
- 1. It can plot any or all quadrants. Shifted origins also handled.
-
- 2. I try to act smart when printing numbers along the axes. If I figure that
- the numbers are whole numbers (xx.00) then I print integers, otherwise I
- print real numbers with two significant digits. You can easily edit the
- file if you want more or less significant digits.
-
- 3. Since Pascal is very unfriendly when it comes to handling character strings
- the syntax of the parameters is a bit unfriendly too. Also the max string
- size that I can handle is 80 chars, and if you need more than that, you
- can give less to the program and then edit the output file without any
- difficulty. I give a lot of comments at appropriate places (all begin with
- a % since that is a latex comment char), you can safely get rid of them in
- your final document.
-
- 4. What I regard as a misfeature in LaTeX (could pass for just a quirk), the
- \caption command ALWAYS prints the "Figure 2.2" in normalsize no matter
- what your environment fonttype is or what you specify in the caption
- arguement. So, to get around it I have a command that explicitly puts a
- caption-look-alike caption at the bottom of the figure in the specified
- font. If you choose to print LaTeX caption then I force a normalsize
- caption but if you want everything in another font then use "explicit"
- option on the ??caption parameter. Since LaTeX decides the figure number
- by itself, I ignore the "fignumber" parameter if you chose the "LaTeX"
- option; on the other hand I ignore the "labelname" parameter if you chose
- the "explicit" option. If you need things other than what the program
- gives you you can explicitly edit the file.
-
- 5. From my experience & personal preference, font "small" or even smaller
- looks nice on the graph; normalsize comes out a bit too big and looks
- unwieldly, specially for printing large real numbers along the Y-axis.
-
- 6. If you are going to have many such graphs in one document, then doing
- the following will make life easier:
- a. have \newcommand{\pcharA}{}
- ...........
- \newcommand{\pcharF}{}
- \newcommand{\xaxis}{}
- \newcommand{\yaxis}{}
- in the preamble of the document; this just initializes the name to null.
- b. change all subsequent "\newcommand"s in each of the graphs to
- "\renewcommand".
-
- 7. The syntax for running the program is
-
- latexgraph <infile >outfile (use outfile.tex if standalone)
-
- infile should have, besides the parameters, only the data lines with
- 1 capital letter and 2 numbers on each line:
-
- LETTER1 Xreal2 Yreal3
-
- LETTER1: range= A..F
- it is the code for the kind of character to be plotted at the
- specified coordinates.
- Xreal2 : is the X-coordinate of the point to be plotted, a real number.
- Yreal3 : is the Y-coordinate of the point to be plotted, a real number.
-
- For input file, a file-name-extension of the form ".gra" or ".in" could
- be used with a ".tex" for the output. The output file(s) can
- subsequently be simply "\input" into the main document, if any.
-
- 8. The numbers along the axes are always below the X-axis and to the left
- of Y-axis. If you desire them differently, for intance, in the case of a
- 2nd quadrant graph, then you will have to manually change the coordinates
- in the output file. (I didn't feel like adding yet another option!).
-
- 9. However ugly the scheme of taking parameters may appear, it has many
- advantages and that's why it was decided upon:
- a. Some of the options could easily be implemented as command line
- options but then you may not remember which graph has what options.
- With the implemented scheme, all the information required for a graph
- can be put into one file and forgotten about.
- b. portability of the program. many computers do not have unix(tm) like
- facilities for command-line options.
-
- 10. I have been asked often, how about providing for "joining the dots".
- Well, there is a companion (actually, quite independent of this program)
- TeX macro package called "epic" authored by myself. It is implemented as a
- documentstyle option and contains several high-level commands for the
- picture environment including line-drawing commands for dotted, dashed, and
- solid lines. It also contains macros that define corresponding
- environments. All these line drawing commands draw straight line segments
- (no smooth curves). Instead of describing things in more details, I suggest
- you acquire that package and read the corresponding documentation, which
- contains an example of how to add line-drawing commands to the output of
- this program. You may obtain the "epic" package from the same source as you
- obtained this, or send mail to me; I perfer that the former channel be
- explored first. Due to lack of motivation and fear of further complicating
- things, I have not implemented options in this program that interface with
- the "epic" package. Hence, you will have to explicitly edit the output to
- incorporate commands to join the dots; I assure you it is very easy and
- requires very minimal editing.
-
- Syntax of the parameters:
- -------------------------
-
- 1. All parameters are recognized by the string of 10 characters at the
- beginning and exactly 10 characters -- no more -- no less. They all
- begin with a % sign because that is the comment character for latex. The
- "%" is quite unnecessary, but remains for historical reasons.
-
- 2. When the arguement for a parameter is an integer then there MUST be atleast
- one blank after the parameter name and for more than one integer, again
- blank char is the separater.
-
- 3. When the arguement for a parameter is a string then there MUST BE NO BLANK
- and instead it should be a /. The string that follows the / is taken
- exactly as it appears starting from the next character including any
- blanks. All that is important is that the 11th char be a /.
-
- 4. There should be no blank lines before or in the middle until the string
- '%%%%%%%%%%' . That string of 10 %'s ends the list of arguements. I don't
- think there should be any blank lines anywhere at all.
-
- 5. The list of parameters SHOULD be the first thing in the file , that is,
- right on the top followed by data, if any. Without any data, you will
- simply get the skeleton of the graph with all things laid out. This is
- one way to make use of this program if you already have explicit statements
- for plotting the points; first get the graph skeleton, and then insert
- your file with latex commands that plot points at the appropriate place.
-
- 6. There are 6 parameters for plotting chars at the specified coordinates.
- That means with this program you can have 6 kinds of data plotted in one
- graph. The parameters are %plotcharA .. %plotcharF. Each parameter has
- a LETTER at the end and that is very significant. The data to be plotted
- must have as its first argument a LETTER A..F that directly corresponds
- to the plotchar# defined in the parameter list. E.g. the data line
- B 20 30 => put plotcharB at (X,Y)=(20,30) on the graph. The idea is that
- I imagine that you will have some program that produces numbers to be
- plotted and you can have that program produce the 3-tuples needed for
- this program very easily.
- There are five corresponding plotname's too. These are short strings
- of upto 25 characters that are used in the legendbox for names of the
- plotchar symbols. You ought to keep them short else the box will become
- too big.
-
- You do not have to have all the 6 char & names. In the input file, you
- may have only as many of the six set as necessary. By default, the
- unlisted char is a 'no'.
-
- 7. For some options there are default values, hence you need not have them
- in the input file unless values other than the default ones are desired.
- The default values are (only for the ones listed here):
- %pre&post?/no
- %unitlngth 1.0 (i.e. 1mm)
- %font-name/{normalsize}
- %picdimens 100 100 0 0
- %??caption/no
- %labelname/no
- %legendloc/no
- %Xratiog:r 10 10 (* 1 : 1 *)
- %Xdeltab:n 5 10 (* numbers on alternate bars *)
- %Xoriginum 0
- %Yratiog:r 10 10 (* 1 : 1 *)
- %Ydeltab:n 5 10 (* numbers on alternate bars *)
- %Yoriginum 0
- %plotcharA/no
- %plotnameA/A
- %plotcharB/no
- %plotnameB/B
- .....
- %plotcharF/no
- %plotnameF/F
-
- The options are as follows:
-
- command arguments explanation
- ------- --------- -----------
-
- %pre&post?/ yes writes the preamble & post-amble alongwith
- the graph. idea is to have a standalone
- graph i.e. a complete document.
- no doesn't write any preamble or post-amble.
- starts with \begin{figure}......
- & ends with \end{figure}.
-
- %unitlngth mm real the unitlength for the graph.
-
- %font-name/ {fontname} one of the many fontnames that LaTeX provides,
- such as normalsize,tt,small, etc. All the
- printed stuff on the graph will be in the spe-
- cified font. MUST put the {} around fontname.
-
- %picdimens picwd picht Xorigin Yorigin
- All the four must be integers and in graph units. All four
- must be specified even if they are 0. max graph height about
- 200mm or so. max graph width is about 240mm. For larger than
- about 170mm you need to do -L on dvi-imagen, which also means
- you have to print this page separately, since just a page
- can't be rotated by latex or dvi-imagen.
-
- Xorigin & Yorigin have special significance and should be
- chosen with care.
- To understand business about [XY]origin you should have read
- the details about origin of picture environment in the LaTeX
- book. [XY]origin refers to the coordinate of the bottom-left
- corner of the whole picture box in which the graph resides.
- Together with the width and the height of the picture box,
- they determine the layout of the graph as to which of the
- quadrants constitute the graph and what is the desired
- origin of the graph. Note that [XY]origin refer to the
- origin of the overall picture box and not of the underlying
- graph. E.g.
-
- %picdimens 100 100 -50 -50
- denotes a picture box of size 100x100 whose bottom left
- corner is (-50,-50) and thus the graph origin (0,0) lies in
- the middle of the picture box. This graph will have all four
- quadrants.
-
- %picdimens 100 100 -100 0
- denotes a picture box of size 100x100 whose bottom left
- corner is at (-100,0) and thus the graph origin (0,0) lies
- at the bottom-right corner. This graph will be only the 2nd
- quadrant.
-
- %picdimens 100 100 50 50
- denotes a picture box of size 100x100 whose bottom left
- corner is at (50,50) and so is its graph origin. This is a
- shifted 1st quadrant.
-
- One way to decide the 4 numbers for %picdimens is to draw on
- a piece of paper a rectangle and decide where you want your
- graph origin and what are its coordinates. Then figure out
- what are the coordinates of the bottom-left corner of the
- enclosing rectangle.
-
- %Xaxisstrg/ string max char 80.for xaxis to appear in the legend
- box and/or in the caption.
- It should be kept small so as not to make
- the box too large.
-
- %Yaxisstrg/ string for yaxis ................. same as above.
-
- %??caption/ LaTeX produce LaTeX command for making caption.
- explicit make an explicit box for a fake caption-look-
- alike caption. see discussion above.
- no no caption of whatever kind.
-
- %fignumber/ string max char = 10.
- the number you wish to give this graph to
- appear on the fake-caption at bottom. This
- is ignored if ??caption=LaTeX.
-
- %capstring/ YX
- string explicit string of less than 80 characters.
- option YX will print the string of 'Y vs. X'
- where Y & X are same as the ones specified
- in the Xaxisstrg & Yaxisstrg above.
-
- %labelname/ {labelname} the name of label you wish to give to the
- figure. This is ignored if you did not choose
- LaTeX option on ??caption. MUST have {} around
- the label name. Also LaTeX doesn't like blanks
- in the name of a label.
- no no \label statement desired.
-
- %legendloc/[location option letters][/ X Y]
-
- The box is my favorite entity in this program and is very
- versatile and fun. "location option letters" are any
- combination of t, b, l, r letters, each occurring max
- once. These options are identical to general options on
- the LaTeX \makebox command and are interpreted likewise;
- In addition, if you do not want the box at all, then write
- "no" after the first /.
- By default the option is to center along unspecified axis.
- The idea is that you can specify one of nine points as the
- reference point for location of the box:
-
- t
- tl +-----+-----+ tr t : top
- | | b : bottom
- l + + + r l : left
- | | r : right
- bl +-----+-----+ br blank: center
- b
-
- The ordering is unimportant. You also have an option of
- explicitly specifying the coordinates in conjunction with
- the specified reference point. The syntax is that if there
- are no arguement besides reference letters (upto 2), then I
- will place it appropriately on the graph, in the region
- specified by the option letters. If you do wish to specify
- explicit coordinates, then the reference letters, if any,
- must be immediately followed by a / , then X and then Y
- coordinates; e.g.
-
- %legendloc/tl --> place the legend box in the top left corner of
- the graph (I do leave some space between the
- axes and the box).
- %legendloc/tl/ 20 80 --> place the top-left corner of box at (20,80).
- %legendloc// 50 50 --> place the box such that its center is at (50,50).
- %legendloc/r/ 50 50 --> place the box such that center of the right edge
- (denoted by r in pic above) is at (50,50). If you
- did not specify 50,50, then I will calculate and
- place it exactly at the center-right of the graph.
- %legendloc/no --> no box at all.
-
- NOTE: the second / should be there only if X & Y integer
- coordinates are specified, else the program will bomb.
-
- %Xratiog:r integer1 real2
- g:r means graph vs. reality; i.e. to say that
- integer1 # of units on graph represent real2
- units of reality; e.g. 10 25 means each 10
- units on graph = 25 reality units.
- this ratio is used to translate the data from
- reality numbers to graph units.
-
- %Xorig-num integer/real The number that corresponds to the origin
- along X-axis.
- %Xdeltab:n integer integer In the b:n, b -> bar, n-> number
- units units the first integer specifies the increment
- used for putting vertical bars (|) on the
- X-axis starting from origin and can be
- different from Xdeltanum (see next parameter).
- should be specified in graph units.
-
- The second integer specifies the increment
- used for putting numbers along the X-axis.
- It can be different from the first integer.
- Though there is no restriction, but normally
- one puts them under the vertical bars or
- perhaps alternate bars.
- Should be specified in graph units. Translated
- to numbers using Xratiog:r. see example below.
-
- %Yratiog:r integer real -----same as for corresponding X....
-
- %Yorig-num integer The number that corresponds to the origin
- along Y-axis.
- %Ydeltab:n integer same story for these three as above.
-
- %plotcharA/ {definition of character number A to be plotted}
- no these specify the LaTeX statement for the
- definition of the character to be put at the
- coordinates, e.g. {circle*{1.0}}
- no: means no character with the number
- associated with plotchar.
- see discussion above.
- %plotnameA/ string name for the plotcharA symbol used in the
- legendbox; is valid only if %plotcharA
- is not equal to 'no'.
- %plotcharB/ {definition of character number B to be plotted}
- no
- %plotnameB/ string name for character B.
- ...
- ...
- %plotcharF/ {definition of character number F to be plotted}
- no
- %plotnameF/ string name for character F.
-
- %%%%%%%%%% THIS LINE OF 10 %'s IS A MUST. IT ACTS AS THE
- DELIMITER OF THE ABOVE PARAMETERS.
- data follows after above parameters.
-
- -----------------------------------------------------------------------------
- An example of an inputfile:
- -----------------------------------------------------------------------------
- %pre&post?/yes
- %unitlngth 1.0
- %font-name/{footnotesize}
- %picdimens 140 120 -70 -60
- %Xaxisstrg/Number of Hops.
- %Yaxisstrg/\%age of Hops
- %??caption/Latex
- %fignumber/1-1
- %capstring/Half-Length of Symmetric Intervals as \%-age of Lifetime means
- %labelname/{my-graph-1}
- %legendloc/tr
- %Xratiog:r 10 10
- %Xdeltab:n 5 10
- %Xoriginum 0
- %Yratiog:r 10 10
- %Ydeltab:n 5 10
- %Yoriginum 0
- %plotcharA/{\circle*{1.0}}
- %plotnameA/Message Switching
- %plotcharB/{\makebox(0,0){$\otimes$}}
- %plotnameB/Circuit Switching
- %plotcharD/no
- %plotcharE/no
- %%%%%%%%%%
- A 20 20
- A 20 -20
- A -20 20
- A -20 -20
- B 30 10
- B 30 -10
- B -30 10
- B -30 -10
- ------------------------------------------------------------------------------
- The output produced by the above input:
- ------------------------------------------------------------------------------
- \documentstyle{article}
- \setlength{\topmargin}{0cm}
- \setlength{\headheight}{1cm}
- \setlength{\footskip}{3cm}
- \setlength{\oddsidemargin}{0cm}
- \setlength{\textwidth}{170.00mm}
- \pagestyle{empty} % => no page number
- \begin{document}
-
- \newcommand{\xaxis}{Number of Hops. } % the literal for X-axis
- \newcommand{\yaxis}{\%age of Hops} % the literal for Y-axis
- \newcommand{\pcharA}{\circle*{1.0}}
- \newcommand{\pcharB}{\makebox(0,0){$\otimes$}}
-
- \begin{footnotesize}
- \begin{figure}[p] %you might want different options here
- \unitlength = 1.00mm
- \begin{center}
- \begin{picture}(150,130)(-80,-70)
- \thicklines
- \put(0,0){\vector(1,0){70}}
- \put(72,0){\makebox(0,0)[l]{X}}
- \put(0,0){\vector(-1,0){70}}
- \put(-72,0){\makebox(0,0)[r]{X}}
- \put(0,0){\vector(0,1){60}}
- \put(0,62){\makebox(0,0)[b]{Y}}
- \put(0,0){\vector(0,-1){60}}
- \put(0,-62){\makebox(0,0)[t]{Y}}
- \thinlines
- \multiput(0,-1)(5,0){14}{\line(0,1){2}}
- \multiput(0,-1)(-5,0){14}{\line(0,1){2}}
- \multiput(-1,0)(0,5){12}{\line(1,0){2}}
- \multiput(-1,0)(0,-5){12}{\line(1,0){2}}
- % Add a line similar to next one if number at origin desired.
- \put(10,-2){\makebox(0,0)[t]{10}}
- \put(20,-2){\makebox(0,0)[t]{20}}
- \put(30,-2){\makebox(0,0)[t]{30}}
- \put(40,-2){\makebox(0,0)[t]{40}}
- \put(50,-2){\makebox(0,0)[t]{50}}
- \put(60,-2){\makebox(0,0)[t]{60}}
- \put(-10,-2){\makebox(0,0)[t]{-10}}
- \put(-20,-2){\makebox(0,0)[t]{-20}}
- \put(-30,-2){\makebox(0,0)[t]{-30}}
- \put(-40,-2){\makebox(0,0)[t]{-40}}
- \put(-50,-2){\makebox(0,0)[t]{-50}}
- \put(-60,-2){\makebox(0,0)[t]{-60}}
- \put(-2,10){\makebox(0,0)[r]{10}}
- \put(-2,20){\makebox(0,0)[r]{20}}
- \put(-2,30){\makebox(0,0)[r]{30}}
- \put(-2,40){\makebox(0,0)[r]{40}}
- \put(-2,50){\makebox(0,0)[r]{50}}
- \put(-2,-10){\makebox(0,0)[r]{-10}}
- \put(-2,-20){\makebox(0,0)[r]{-20}}
- \put(-2,-30){\makebox(0,0)[r]{-30}}
- \put(-2,-40){\makebox(0,0)[r]{-40}}
- \put(-2,-50){\makebox(0,0)[r]{-50}}
- \put(70,60){\makebox(0,0)[tr]{\fbox{\shortstack[l]{
- {\makebox(4,2)[lb]{\put(2,1){\pcharA}}}: Message Switching\\
- {\makebox(4,2)[lb]{\put(2,1){\pcharB}}}: Circuit Switching\\
- {\makebox(4,4)[b]{X}}: \xaxis \\
- {\makebox(4,2)[b]{Y}}: \yaxis
- }}}}
- % beginning of data
- \put(20.00000,20.00000){\pcharA}
- \put(20.00000,-20.00000){\pcharA}
- \put(-20.00000,20.00000){\pcharA}
- \put(-20.00000,-20.00000){\pcharA}
- \put(30.00000,10.00000){\pcharB}
- \put(30.00000,-10.00000){\pcharB}
- \put(-30.00000,10.00000){\pcharB}
- \put(-30.00000,-10.00000){\pcharB}
- % end of data
- \end{picture}
- \end{center}
- % if the caption line is longer than the graphwidth,use a
- % \parbox[t]{...mm}{.......} like statement for the argument
- % with suitable args for parbox to get things centered.
- \caption{\protect\normalsize
- Half-Length of Symmetric Intervals as \%-age of Lifetime means
- }
- \label{my-graph-1}
- \end{figure}
-
- \end{footnotesize}
-
- \end{document}
- ------------------------------------------------------------------------------
- END OF DOC.
-